<template>
	<view class="ayi-page">
		<view class="bg-c"><image class="bg-img" mode="aspectFill" src="../../../static/img/bg.png"></image></view>
		<view class="login-form">
			<view class="login-back" @click="backHome()"><i class="iconfont">&#xe63d;</i></view>
			<view class="login-icon"><image style="width: 80px; height: 140px;" mode="aspectFit" src="../../../static/img/login_icon.png"></image></view>
			<view class="login-f-control">
				<i class="iconfont">&#xe671;</i>
				<input class="l-input" v-model="username" placeholder="用户名" />
			</view>
			<view class="login-f-control">
				<i class="iconfont">&#xe6ad;</i>
				<input class="l-input" v-model="psw" type="password" placeholder="密码" />
			</view>
			<view class="login-btn-control">
				<button type="primary" @click="loginIn()" class="l-btn-c">登陆</button>
				<button type="primary" @click="play()" class="l-btn-c">试玩</button>
			</view>
			<view class="login-btn-a-control">
				<text class="a-btn" @click="findPsw()">忘记密码?</text>
				<text class="a-btn" @click="regist()">注册</text>
			</view>
		</view>
	</view>
</template>

<script>
import { Tools, Config, URL, Net, VueTools } from '@/common/tools.js';
export default {
	data() {
		return {
			username: '',
			psw: ''
		};
	},
	onLoad() {
		// #ifdef APP-PLUS
		this.Tab.getInstance().hide();
		// #endif
	},
	methods: {
		backHome() {
			this.$store.commit('setTabbarIndex', 3);
			this.isShowTabMask();
			if (!Config.is3rd) {
				uni.switchTab({
					url: '/pages/home/home',
					complete: () => {
						// #ifdef APP-PLUS
						this.Tab.getInstance().show(false);
						// #endif
					}
				});
			} else {
				uni.switchTab({
					url: '/pages/home/home3rd',
					complete: () => {
						// #ifdef APP-PLUS
						this.Tab.getInstance().show(false);
						// #endif
					}
				});
			}
		},
		findPsw() {
			//找回密码
			this.navigateToSigleUrl('/pages/my/findpsw/findpsw');
		},
		regist() {
			//注册
			if (Config.isAgent == 1) {
				this.navigateToSigleUrl('/pages/my/registagent/registagent');
			} else {
				this.navigateToSigleUrl('/pages/my/regist/regist');
			}
		},
		loginIn() {
			//登陆
			if (Tools.isEmpty(this.username)) {
				VueTools.toastNo('请输入用户名');
				return;
			}
			if (Tools.isEmpty(this.psw)) {
				VueTools.toastNo('请输入密码');
				return;
			}
			let that = this;
			Net.ayiHttp(
				URL.LOGIN_LOGIN,
				'{"username":"' + this.username + '","password":"' + this.psw + '"}',
				function(res) {
					if (res.code == Config.SUCCESS_CODE) {
						//成功的逻辑
						let data = Tools.aesDecodeToJson(res.data);
						VueTools.clearStorageSync();
						VueTools.setStorageSync('token', data['access-token']);
						VueTools.setStorageSync('sessid', data['sessid']);
						VueTools.setStorageSync(Config.FUID_KEY, data['fuid']);
						VueTools.setStorageSync(Config.IS_LOGIN_STORE_KEY, '1');
						VueTools.setStorageSync(Config.ROLE_KEY, data['role']);
						//隐藏tabbar-mask的组件
						uni.$emit('isShowTabMask', { isShow: false });
						//更新菜单数据
						//获取用户信息并存储
						that.userInfo();
						//存储未结算和今日余额
						that.getMoney(function(res) {});
						that.backHome();
						console.log('处理好了的', data);
					} else {
						VueTools.toastNo(res.message);
					}
				},
				function(err) {
					console.log('error:', err);
				}
			);
		},
		play() {
			//试玩
			let that = this;
			Net.ayiHttp(
				URL.LOGIN_TESTPLAY,
				'',
				function(res) {
					if (res.code == Config.SUCCESS_CODE) {
						//成功的逻辑
						let data = Tools.aesDecodeToJson(res.data);
						VueTools.clearStorageSync();
						VueTools.setStorageSync('token', data['access-token']);
						VueTools.setStorageSync('sessid', data['sessid']);
						VueTools.setStorageSync(Config.FUID_KEY, data['fuid']);
						VueTools.setStorageSync(Config.IS_LOGIN_STORE_KEY, '1');
						VueTools.setStorageSync(Config.ROLE_KEY, data['role']);
						//隐藏tabbar-mask的组件
						uni.$emit('isShowTabMask', { isShow: false });
						//更新菜单数据
						//获取用户信息并存储
						that.userInfo();
						//存储未结算和今日余额
						that.getMoney(function(res) {});
						that.backHome();
						console.log('处理好了的', data);
					} else {
						VueTools.toastNo(res.message);
					}
				},
				function(err) {
					console.log('error:', err);
				}
			);
		},
		userInfo() {
			//存储用户的信息数据
			let that = this;
			Net.ayiHttp(
				URL.USER_SHOWUSERINFO,
				'',
				function(res) {
					if (res.code == Config.SUCCESS_CODE) {
						//成功的逻辑
						let data = Tools.aesDecodeToJson(res.data);
						VueTools.setStorageSync(Config.USERINFO_KEY, data);
						console.log('登陆成功获取的数据', data);
					} else {
						VueTools.toastNo(res.message);
					}
				},
				function(err) {
					console.log('error:', err);
				}
			);
		}
	},
	onUnload() {}
};
</script>

<style lang="scss">
.bg-c {
	position: fixed;
	top: 0px;
	left: 0px;
	right: 0px;
	bottom: 0px;
	z-index: -1;
	.bg-img {
		width: 100%;
		height: 100%;
	}
}
.login-form {
	display: flex;
	justify-content: center;
	flex-direction: column;
	align-items: center;
	.login-back {
		text-align: left;
		color: #fff;
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		padding-left: 20rpx;
	}
	.login-icon {
		margin-bottom: 20px;
	}
	.login-f-control {
		margin-bottom: 10px;
		height: 40px;
		display: flex;
		align-items: center;
		i.iconfont {
			font-size: 24px;
			color: #fff;
			margin-right: 10px;
		}
		.l-input {
			height: 40px;
			width: 600rpx;
			color: #ffffff;
			border-bottom: 1px solid #ffffff;
			display: inline-block;
		}
		.uni-input-input {
			background: red;
		}
	}
	.login-btn-control {
		width: 680rpx;
		margin-top: 20px;
		.l-btn-c {
			background: $wlh-color-defealt;
			margin-bottom: 10px;
		}
	}
	.login-btn-a-control {
		width: 680rpx;
		margin-top: 20px;
		display: flex;
		justify-content: space-between;
		.a-btn {
			color: #fff;
		}
	}
}
</style>
